var pager = {
	option: {
		url: '',
		container: '',
		tpl_id: 'template',
		pager: '', //分页容器
		search_form: '',	//查询表单
		search_btn: '' //查询按钮
	},
	total: 0,
	page_size: 10,
	cur_page: 1,
	setOption: function(options){
		for (var index in options) {
			this.option[index] = options[index];
		}
		//console.log(this.option);
		//事件委托
		var _this = this;
		$(this.option.pager).on('click','li a',function(){
	        var rel = $(this).attr("rel");
	        if(rel){
	           _this.getData(rel);
	        }
	    });
	    //console.log($(this.option.search_btn));
	    //按钮
	    $(this.option.search_btn).click(function(){
			_this.getData(1);
		})
		$(this.option.search_form).on('change','select',function(){
			_this.getData(1);
		})
	},
	getData: function(page) {

		var opt = this.option;
		var _this = this;
		//console.log(opt);
		if (!opt.url) {
			alert('缺少url');
			return;
		}
		if (!opt.container) {
			alert('缺少container');
			return;
		}
		if ($(opt.container).length ==0) {
			alert('container无效');
			return;
		}
		
		var search = {};
		if (opt.search_form) {	//查询条件
			search = getFormData(opt.search_form);
		}
		//console.log(search);

		$.ajax({
            url: opt.url + "?page=" + page,
            type:'POST',
            dataType: 'json',
            data: search,
            beforeSend:function(){
                 
            },
            success:function(rsp){
                _this.total = rsp.data.total; //总记录数
                _this.page_size = rsp.data.page_size; //每页显示条数
                _this.cur_page = rsp.data.page; //当前页
                //console.log(_this.option);
                var data_html = template(opt.tpl_id,rsp.data);
                //console.log(data_html);
	    		$(opt.container).html(data_html);
            },
            complete:function(){ //生成分页条
                var page_html = _this.getPageNav(_this.total,_this.page_size,_this.cur_page);

                if ($(opt.pager).length ==0) {
                	alert('缺少分页容器');
                }
                $(opt.pager).html(page_html);
            },
            error:function(){
                alert("数据加载失败");
            }
        });
	},
	refreshData: function(){
		var page = $(this.option.pager).find('.active a').text();
		this.getData(page);
	},
	getPageNav: function(total,page_size,cur_page) {
		var total_page = 0; //总页数
		if (total==0) {	
			return '';
		}
		total_page = Math.ceil(total/page_size);

		cur_page = Number(cur_page);
		var start = 0;	//开始页码
		var end = 0;	//结束页码
		if (cur_page-5>=1) {
			start = cur_page-5;
		} else {
			start = 1;
		}
		if (cur_page+5<=total_page) {
			end  = cur_page+5;
		} else {
			end = total_page;
		}
		//console.log(total,page_size,cur_page,total_page,start,end);
		var page_nav = ['<ul class="pagination">'];
		if (cur_page !=1) {
			page_nav.push('<li><a href="#" rel="1">首页</a></li>');
			page_nav.push('<li><a href="#" rel="' + (cur_page-1) + '">上一页</a></li>');
		}
		if (start != end) {
			for(i=start;i<=end;i++) {
				if (cur_page == i) {
					page_nav.push('<li class="active"><a href="#" rel="' + i + '">' + i + '</a></li>');
				} else {
					page_nav.push('<li><a href="#" rel="' + i + '">' + i + '</a></li>');
				}
			}
		}
        if (cur_page != total_page) {
        	page_nav.push('<li><a href="#" rel="' + (cur_page+1) + '">下一页</a></li>');
        	page_nav.push('<li><a href="#" rel="' + total_page + '">尾页</a></li>');
        }
       	page_nav.push('</ul>');
		return page_nav.join('');
	}
}
